<template>
	<view class="bd_container cal">
		<!-- 封面 -->
		<view class="bd_top cal">
			<view class="bd_left">
				<image src="@/static/novel1.jpg" mode="widthFix"></image>
			</view>
			<view class="bd_right">
				<view class="bd_title">{{name}}</view>
				<view class="bd_author">作者：{{author}}</view>
				<view class="bd_read">
					<image src=" @/static/reading.svg" mode="widthFix">
					</image>
					<text>阅读</text>
				</view>
				<view class="bd_collect">
					<image src=" @/static/collect.svg" mode="widthFix">
					</image>
					<text>收藏</text>
				</view>
			</view>
		</view>

		<!-- 介绍 -->
		<view class="bd_intro">
			<!-- 导航 -->
			<view class="tab_row shadow">
				<block v-for="(item, index) in tabArr" :key="index" >
					<view class="tab_box" :class="{ active: tabIndex === index }" @tap="changeTabIndex(index)">
						<view class="tab_top">{{item.name}}</view>
						<view class="tab_border" v-if="tabIndex === index"></view>
					</view>
				</block>
			</view>
			<!-- 详情 synopsis -->
			<scroll-view scroll-y v-if="tabIndex === 0" class="sy_container">
				<view class="sy_box">
					<view class="sy_top">
						—— 书籍详情 ——
					</view>
					<view v-html="synopsis" class="sy_main"></view>
				</view>
			</scroll-view>
			<!-- 目录  catalog -->
			<scroll-view scroll-y v-else class="cata_container">
				<view v-for="chapter in catalog" :key="chapter.id" class="cata_box">
					<view class="cata_main" @tap="toReading(chapter.id)">{{chapter.title}}</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import test from '@/common/test';
	import $U from '@/common/unit.js';
	
	export default {
		data() {
			return {
				calHeight: 0,
				tabIndex: 0,
				tabArr: [{
						name: '详情'
					},
					{
						name: '目录'
					}
				],
				// 使用从 test.js 导入的内容
				// 详情
				name: test.name,
				author: test.author,
				synopsis: test.synopsis,
				// 目录
				catalog: test.chapterCatalog
			};
		},
		methods: {
			changeTabIndex(index) {
				// console.log("切换到 tab 索引: ", index);
				this.tabIndex = index; // 更新当前激活的tab
			},
			toReading(id) {
				uni.navigateTo({
					// 传值
					url: `/pages/bookReading/bookReading?chapterID=${id}`
				});
			}
		},
		mounted() {
			$U.calSurplusHeight({
				pageID: this,
				pos: 'cal',
				success: val => this.calHeight =val
			})
		}
	}
</script>

<style lang="scss">
	.bd_container {

		// 封面
		.bd_top {
			padding: 30rpx 30rpx 24rpx 30rpx;
			display: flex;
			// 设置渐变背景色
			background: linear-gradient(to right, #95a9bf, #b8c5d4, #d0d7dd, #e7e7e6, #f2f2f2);

			// 左
			.bd_left {
				width: 34%;

				image {
					width: 180rpx;
					height: 260rpx;
					border-radius: 10rpx;
				}
			}

			// 右
			.bd_right {
				width: calc(100% - 34%);

				// 上
				.bd_title {
					font-size: 34rpx;
					font-weight: bold;
					padding: 0 0 20rpx 0;
				}

				// 中
				.bd_author {
					font-weight: bold;
					font-size: 30rpx;
					padding: 0 0 10rpx 0;
					font-family: 'SimSun', '宋体', sans-serif;
				}

				// 阅读
				.bd_read {
					padding: 0 0 10rpx 20rpx;

					image {
						width: 48rpx;
						height: 48rpx;
						transform: translate(-30%, 26%);
					}
				}

				// 收藏
				.bd_collect {
					padding-left: 20rpx;

					image {
						width: 48rpx;
						height: 48rpx;
						transform: translate(-30%, 22%);
					}
				}
			}
		}

		// 介绍
		.bd_intro {

			// 导航
			.tab_row {
				display: flex;
				align-items: center;
				border-bottom: 1rpx solid #ccc;
				height: 80rpx;
				justify-content: space-around;

				.tab_box {
					display: flex;
					flex-direction: column;
					height: 80rpx;
					position: relative;
					cursor: pointer;
					transition: background-color 0.3s;

					.tab_top {
						width: 80rpx;
						text-align: center;
						transform: translate(0, 34%);
					}

					.tab_border {
						width: 80rpx;
						height: 8rpx;
						border-radius: 4rpx;
						background-color: #fb5f39;
						position: absolute;
						bottom: 0;
					}
				}

				/* 激活样式 */
				.tab_box.active {
					font-size: 34rpx;
					color: #fb5f39;
				}
			}

			// 详情
			.sy_container {
				.sy_box {
					padding: 20rpx 30rpx;

					.sy_top {
						font-size: 36rpx;
						text-align: center;
						padding-bottom: 18rpx;
						color: #94a8bc;
						// font-weight: bold;
					}

					.sy_main {
						font-size: 35rpx;
						// 行高
						// line-height: 70rpx;
						line-height: 1.999;
					}
				}
			}

			// 目录
			.cata_container {
				.cata_box {
					.cata_main {
						font-size: 36rpx;
						// 上 右 下 左
						padding: 20rpx 0 20rpx 20rpx;
						border-bottom: 1rpx solid #eee;
					}
				}
			}
		}
	}
</style>